/**
 * 
 */
package com.topu.db.model;

/**
 * @author popofish
 * 
 */
public class DBColumn implements IDBColumn {

	private String column;
	private Object defaultValue;
	private String description;
	private DBType type;
	private String property;
	private boolean autoIncrement;
	private boolean notNull;
	private boolean unique;
	private boolean index;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.topu.db.model.IDBColumn#getColumn()
	 */
	@Override
	public String getColumn() {
		return this.column;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.topu.db.model.IDBColumn#getDefault()
	 */
	@Override
	public Object getDefault() {
		return this.defaultValue;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.topu.db.model.IDBColumn#getDescription()
	 */
	@Override
	public String getDescription() {
		return this.description;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.topu.db.model.IDBColumn#getProperty()
	 */
	@Override
	public String getProperty() {
		return this.property;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.topu.db.model.IDBColumn#getType()
	 */
	@Override
	public DBType getType() {
		return this.type;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.topu.db.model.IDBColumn#isAutoIncrement()
	 */
	@Override
	public boolean isAutoIncrement() {
		return this.autoIncrement;
	}

	/**
	 * @return the index
	 */
	public boolean isIndex() {
		return index;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.topu.db.model.IDBColumn#isNotNull()
	 */
	@Override
	public boolean isNotNull() {
		return this.notNull;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.topu.db.model.IDBColumn#isUnique()
	 */
	@Override
	public boolean isUnique() {
		return this.unique;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.topu.db.model.IDBColumn#setAutoIncrement(boolean)
	 */
	@Override
	public void setAutoIncrement(boolean v) {
		this.autoIncrement = v;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.topu.db.model.IDBColumn#setColumn(java.lang.String)
	 */
	@Override
	public void setColumn(String column) {
		this.column = column;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.topu.db.model.IDBColumn#setDefault(java.lang.Object)
	 */
	@Override
	public void setDefault(Object defaultValue) {
		this.defaultValue = defaultValue;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.topu.db.model.IDBColumn#setDescription(java.lang.String)
	 */
	@Override
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * @param index
	 *            the index to set
	 */
	public void setIndex(boolean index) {
		this.index = index;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.topu.db.model.IDBColumn#setNotNull(boolean)
	 */
	@Override
	public void setNotNull(boolean v) {
		this.notNull = v;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.topu.db.model.IDBColumn#setProperty(java.lang.String)
	 */
	@Override
	public void setProperty(String property) {
		this.property = property;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.topu.db.model.IDBColumn#setType(com.topu.db.model.DBType)
	 */
	@Override
	public void setType(DBType type) {
		this.type = type;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.topu.db.model.IDBColumn#setUnique(boolean)
	 */
	@Override
	public void setUnique(boolean v) {
		this.unique = v;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		String msg = "\n\t[";
		msg += "column=" + column;
		msg += ",default=" + defaultValue;
		msg += ",description=" + description;
		msg += ",type=" + type;
		msg += ",property=" + property;
		msg += ",auto=" + autoIncrement;
		msg += ",notnull=" + notNull;
		msg += ",unique=" + unique;
		msg += ",index=" + index;
		return msg + "]";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.topu.db.model.ISqlWrapper#wrapSQL()
	 */
	@Override
	public String wrapSQL() {
		IDBColumn column = this;
		String SQL = " `" + column.getColumn() + "` " + column.getType();
		if (column.isNotNull())
			SQL += "  NOT NULL ";
		if (column.getDefault() != null) {
			if (column.isNotNull() && column.getDefault().equals("NULL")) {
			} else if (!column.isAutoIncrement()) {
				SQL += " DEFAULT " + column.getDefault() + " ";
			}
		}
		if (column.isAutoIncrement())
			SQL += " auto_increment ";
		SQL += " COMMENT '" + column.getDescription() + "'";
		return SQL;
	}

}
